package cn.monkey.family.server.domain.uc.role;

import cn.monkey.family.data.crud.repository.ExampleQueryRequestRepository;
import cn.monkey.family.data.uc.role.Role;
import cn.monkey.family.data.uc.role.RoleQueryRequest;
import com.google.common.base.Strings;
import org.springframework.data.domain.*;
import org.springframework.data.repository.NoRepositoryBean;

@NoRepositoryBean
public interface RoleRepository extends ExampleQueryRequestRepository<Role, String, RoleQueryRequest> {

    @Override
    default Example<Role> example(RoleQueryRequest queryRequest) {
        Role role = new Role();
        String name = queryRequest.getName();
        boolean hasProperty = false;
        ExampleMatcher matcher = ExampleMatcher.matching();
        if (!Strings.isNullOrEmpty(name)) {
            role.setName(name);
            hasProperty = true;
            matcher = matcher.withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains());
        }
        String type = queryRequest.getType();
        if (!Strings.isNullOrEmpty(type)) {
            role.setType(type);
            hasProperty = true;
            matcher = matcher.withMatcher("type", ExampleMatcher.GenericPropertyMatchers.exact());
        }
        return hasProperty ? Example.of(role, matcher) : Example.of(role);
    }


}
